﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using System.IO;

namespace Server
{
    public partial class IrisServer : Form
    {
        private BindingSource viewPorts;
        private UdpClient conn;
        private string configFile = "iris.xml";

        public IrisServer()
        {
            InitializeComponent();
        }

        private void IrisServer_Load(object sender, EventArgs e)
        {
            viewPorts = new BindingSource();
            conn = new UdpClient();
            viewPorts.DataSource = typeof(ViewPort);
            if (File.Exists(configFile))
            {
                LoadConfig(configFile);
                textBoxInterval.Text = trackBarInterval.Value.ToString();
                generateViewPorts();
            }
            else
            {
                MessageBox.Show(configFile + " not found! Please create a config File", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1
                    , MessageBoxOptions.ServiceNotification);
                this.Close();
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            disableCalibrateTimer();
            disableCaptureTimer();
            SaveConfig(configFile);
            timerCapture_Tick(sender, e);
        }

        private void toggleCaptureTimer()
        {
            if (timerCapture.Enabled)
            {
                disableCaptureTimer();
            }
            else
            {
                enableCaptureTimer();
            }
            return;
        }

        private void disableCaptureTimer()
        {
            timerCapture.Enabled = false;
            buttonCapture.Text = "Capture";
        }

        private void enableCaptureTimer()
        {
            timerCapture.Enabled = true;
            buttonCapture.Text = "Stop";
        }

        private void toggleCalibrateTimer()
        {
            if (timerCalibrate.Enabled)
            {
                disableCalibrateTimer();
            }
            else
            {
                enableCalibrateTimer();
            }
            return;
        }

        private void disableCalibrateTimer()
        {
            timerCalibrate.Enabled = false;
            buttonCalibrate.Text = "Calibrate";
        }

        private void enableCalibrateTimer()
        {
            timerCalibrate.Enabled = true;
            buttonCalibrate.Text = "Stop";
        }

        private void LoadConfig(string fileName)
        {
            IrisConfig loader = Helpers.LoadConfig(fileName);
            timerCapture.Interval = loader.PollingInterval;
            timerCalibrate.Interval = loader.PollingInterval;
            viewPorts.DataSource = (BindingList<ViewPort>)loader.ViewPorts;
        }

        private void SaveConfig(string fileName)
        {
            IrisConfig saver = new IrisConfig();
            saver.PollingInterval = timerCapture.Interval;
            saver.ViewPorts = (BindingList<ViewPort>)viewPorts.List;
            Helpers.SaveConfig(saver, fileName);
        }

        private void IrisServer_FormClosing(object sender, FormClosingEventArgs e)
        {
            disableCalibrateTimer();
            disableCaptureTimer();
        }

        private void timerCalibrate_Tick(object sender, EventArgs e)
        {
            foreach (ViewPort vp in viewPorts)
            {
                Byte[] imageByteArray;
                vp.calibrate();
                imageByteArray = vp.Image.ToByteArray(System.Drawing.Imaging.ImageFormat.Jpeg);
                conn.Send(imageByteArray, imageByteArray.Length, vp.Host, vp.Port);
            }
        }

        private void timerCapture_Tick(object sender, EventArgs e)
        {
            foreach (ViewPort vp in viewPorts)
            {
                Byte[] imageByteArray;
                vp.capture();
                imageByteArray = vp.Image.ToByteArray(System.Drawing.Imaging.ImageFormat.Jpeg);
                conn.Send(imageByteArray, imageByteArray.Length, vp.Host, vp.Port);
            }
        }

        private void trackBarInterval_Scroll(object sender, EventArgs e)
        {
            timerCalibrate.Interval = 1000 / trackBarInterval.Value;
            timerCapture.Interval = 1000 / trackBarInterval.Value;
            textBoxInterval.Text = trackBarInterval.Value.ToString();
        }

        private void buttonCapture_Click(object sender, EventArgs e)
        {
            disableCalibrateTimer();
            toggleCaptureTimer();
        }

        private void buttonCalibrate_Click(object sender, EventArgs e)
        {
            disableCaptureTimer();
            toggleCalibrateTimer();
        }

        private void generateViewPorts()
        {
            const int width5 = 32;
            foreach (ViewPort vp in viewPorts)
            {
                // create a new tab page and add it to the tabcontroller
                vp.capture();
                PictureBox pBox = new PictureBox();
                pBox.MaximumSize = new Size(600, 600);
                pBox.Top = 22;
                pBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
                pBox.DataBindings.Add("Image", vp, "Image");

                TextBox capX = new TextBox();
                capX.DataBindings.Add("Text", vp, "ScreenCaptureX", false, DataSourceUpdateMode.OnValidation);
                capX.Width = width5;
                capX.MaxLength = 5;

                TextBox capY = new TextBox();
                capY.DataBindings.Add("Text", vp, "ScreenCaptureY", false, DataSourceUpdateMode.OnValidation);
                capY.Width = width5;
                capY.MaxLength = 5;
                capY.Left = capX.Left + capX.Width + 1;

                TextBox sizeX = new TextBox();
                sizeX.DataBindings.Add("Text", vp, "SizeX", false, DataSourceUpdateMode.OnValidation);
                sizeX.Width = width5;
                sizeX.MaxLength = 5;
                sizeX.Left = capY.Left + capY.Width + 1;

                TextBox sizeY = new TextBox();
                sizeY.DataBindings.Add("Text", vp, "SizeY", false, DataSourceUpdateMode.OnValidation);
                sizeY.Width = width5;
                sizeY.MaxLength = 5;
                sizeY.Left = sizeX.Left + sizeX.Width + 1;

                TextBox posX = new TextBox();
                posX.DataBindings.Add("Text", vp, "ScreenPositionX", false, DataSourceUpdateMode.OnValidation);
                posX.Width = width5;
                posX.MaxLength = 5;
                posX.Left = sizeY.Left + sizeY.Width + 1;

                TextBox posY = new TextBox();
                posY.DataBindings.Add("Text", vp, "ScreenPositionY", false, DataSourceUpdateMode.OnValidation);
                posY.Width = width5;
                posY.MaxLength = 5;
                posY.Left = posX.Left + posX.Width + 1;

                TextBox host = new TextBox();
                host.DataBindings.Add("Text", vp, "Host", false, DataSourceUpdateMode.OnValidation);
                host.Width = 90;
                host.MaxLength = 32;
                host.Left = 132;
                host.Left = posY.Left + posY.Width + 1;

                TextBox port = new TextBox();
                port.DataBindings.Add("Text", vp, "Port", false, DataSourceUpdateMode.OnValidation);
                port.Width = 40;
                port.MaxLength = 6;
                port.Left = host.Left + host.Width + 1;

                TextBox name = new TextBox();
                name.DataBindings.Add("Text", vp, "Name", false, DataSourceUpdateMode.OnValidation);
                name.Width = 90;
                name.MaxLength = 32;
                name.Left = port.Left + port.Width + 1;

                TextBox desc = new TextBox();
                desc.DataBindings.Add("Text", vp, "Description", false, DataSourceUpdateMode.OnValidation);
                desc.Width = 128;
                desc.MaxLength = 64;
                desc.Left = name.Left + name.Width + 1;

                Button save = new Button();
                save.Text = "Save";
                save.Height = 20;
                save.Width = 64;
                save.Click += new System.EventHandler(this.buttonSave_Click);
                save.Left = desc.Left + desc.Width + 1;

                TabPage tPage = new TabPage(vp.Name);
                tPage.Controls.Add(pBox);
                tPage.Controls.Add(capX);
                tPage.Controls.Add(capY);
                tPage.Controls.Add(sizeX);
                tPage.Controls.Add(sizeY);
                tPage.Controls.Add(posX);
                tPage.Controls.Add(posY);
                tPage.Controls.Add(host);
                tPage.Controls.Add(port);
                tPage.Controls.Add(name);
                tPage.Controls.Add(desc);
                tPage.Controls.Add(save);
                tabControl.TabPages.Add(tPage);
            }
        }
    }
}
